{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 全局变量和局部变量的传递路径\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "global_x: 8\n"
     ]
    }
   ],
   "source": [
    "# 全局变量\n",
    "global_x = 8\n",
    "print(\"global_x:\", global_x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T15:01:32.354271Z",
     "end_time": "2024-07-03T15:01:32.360209Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# 自定义函数\n",
    "def my_function(local_x):\n",
    "    # local_x 局部变量\n",
    "    print(\"local_x:\", local_x)  # local_x: 38\n",
    "\n",
    "    # 创建一个同名的局部变量\n",
    "    global_x = 88  # global_x: 88\n",
    "    print(\"global_x:\", global_x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T15:02:00.732565Z",
     "end_time": "2024-07-03T15:02:00.740089Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "local_x: 38\n",
      "global_x: 88\n"
     ]
    }
   ],
   "source": [
    "# 调用函数\n",
    "my_function(38)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T15:02:09.686988Z",
     "end_time": "2024-07-03T15:02:09.738011Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "global_x: 8\n"
     ]
    }
   ],
   "source": [
    "# 在函数外部访问全局变量\n",
    "print(\"global_x:\", global_x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T15:02:24.461174Z",
     "end_time": "2024-07-03T15:02:24.468710Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'local_x' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[5], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mlocal_x:\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[43mlocal_x\u001B[49m) \n",
      "\u001B[1;31mNameError\u001B[0m: name 'local_x' is not defined"
     ]
    }
   ],
   "source": [
    "print(\"local_x:\", local_x)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
